package com.grechur.wanandroidcompose.viewmodel

import android.app.Application
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.AndroidViewModel
import com.grechur.wanandroidcompose.bean.coin.CoinBean
import com.grechur.wanandroidcompose.bean.coin.CoinFromBean
import com.grechur.wanandroidcompose.http.NetworkRequest
import com.grechur.wanandroidcompose.http.service.CoinService


class CoinViewModel(application: Application) : AndroidViewModel(application) {

    val coinService = NetworkRequest.getService(getApplication(),CoinService::class.java)

    var myCoin by mutableStateOf(Result.success(CoinBean()))
        private set

    var takeCoins by mutableStateOf(Result.success(listOf<CoinFromBean>()))
        private set

    var takeCurrentIndex by mutableStateOf(0)
        private set

    var rankCoins by mutableStateOf(Result.success(listOf<CoinBean>()))
        private set

    var rankCurrentIndex by mutableStateOf(0)
        private set

    var mineCoinInfo:CoinBean?=null

    suspend fun getMineCoin() {
        val result = try {
            val mineCoin = coinService.getMineCoin()
            if(mineCoin.errorCode == 0) {
                mineCoin.data.takeIf { it != null }?.let { coinBean ->
                    mineCoinInfo = coinBean
                    Result.success(coinBean)
                }?:Result.success(CoinBean())
            } else {
                Result.failure(IllegalArgumentException("error code ${mineCoin.errorCode} msg ${mineCoin.errorMsg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        myCoin = result
    }

    suspend fun takeCoinList(index:Int = 1) {
        val result = try {
            if (index == takeCurrentIndex) {
                return
            }
            val mineCoin = coinService.takeCoinList(index)
            if(mineCoin.errorCode == 0) {
                val default = takeCoins.getOrDefault(listOf()).toMutableList()
                mineCoin.data.takeIf { it != null }?.let { coinPage ->
                    coinPage.datas.takeIf { !it.isNullOrEmpty() }?.let { coinFromBeans ->
                        takeCurrentIndex ++
                        default.addAll(coinFromBeans)
                        Result.success(default)
                    }?:Result.success(default)
                }?:Result.success(default)
            } else {
                Result.failure(IllegalArgumentException("error code ${mineCoin.errorCode} msg ${mineCoin.errorMsg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        takeCoins = result
    }

    suspend fun getCoinRank(index: Int = 1) {
        val result = try {
            if (index == rankCurrentIndex) {
                return
            }
            val mineCoin = coinService.getCoinRank(index)
            if(mineCoin.errorCode == 0) {
                val default = rankCoins.getOrDefault(listOf()).toMutableList()
                mineCoin.data.takeIf { it != null }?.let { coinPage ->
                    coinPage.datas.takeIf { !it.isNullOrEmpty() }?.let { coinBeans ->
                        rankCurrentIndex ++
                        default.addAll(coinBeans)
                        Result.success(default)
                    }?:Result.success(default)
                }?:Result.success(default)
            } else {
                Result.failure(IllegalArgumentException("error code ${mineCoin.errorCode} msg ${mineCoin.errorMsg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        rankCoins = result
    }
}